package com.kiki.comment.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.kiki.comment.domain.CommentVO;
import com.kiki.common.core.domain.entity.SysUser;
import com.kiki.common.utils.DateUtils;
import com.kiki.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kiki.comment.mapper.CommentMapper;
import com.kiki.comment.domain.Comment;
import com.kiki.comment.service.ICommentService;

/**
 * 音乐评论Service业务层处理
 * 
 * @author 林志毅
 * @date 2025-01-08
 */
@Service
public class CommentServiceImpl implements ICommentService 
{
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询音乐评论
     * 
     * @param commentId 音乐评论主键
     * @return 音乐评论
     */
    @Override
    public Comment selectCommentByCommentId(Long commentId)
    {
        return commentMapper.selectCommentByCommentId(commentId);
    }

    /**
     * 查询音乐评论列表
     * 
     * @param comment 音乐评论
     * @return 音乐评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 新增音乐评论
     * 
     * @param comment 音乐评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        comment.setCreateTime(DateUtils.getNowDate());
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改音乐评论
     * 
     * @param comment 音乐评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除音乐评论
     * 
     * @param commentIds 需要删除的音乐评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentIds(Long[] commentIds)
    {
        return commentMapper.deleteCommentByCommentIds(commentIds);
    }

    /**
     * 删除音乐评论信息
     * 
     * @param commentId 音乐评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentId(Long commentId)
    {
        return commentMapper.deleteCommentByCommentId(commentId);
    }

    @Override
    public List<CommentVO> listCommentVo(Long songId) {
        Comment comment1 = new Comment();
        comment1.setSongId(songId);
        List<Comment>commentList=commentMapper.selectCommentList(comment1);

        System.out.println("获取到的coment对象列表"+commentList);
        List<CommentVO> commentVOList=new ArrayList<>();
        for (Comment comment:commentList) {
            CommentVO commentVO=new CommentVO();
            SysUser User = userMapper.selectUserById(comment.getUserId());
            System.out.println("这个用户："+User);
            commentVO.setAvatar(User.getAvatar());
            commentVO.setUserName(User.getUserName());
            commentVO.setContent(comment.getContent());
            commentVO.setCreateTime(comment.getCreateTime());
            commentVOList.add(commentVO);
        }
        System.out.println("获取到的coment对象列表"+commentVOList);
        return commentVOList;
    }

    @Override
    public int getCommentCount(Long songId) {
        return commentMapper.getCommentCount(songId);
    }
}
